package com.my.assembly.handler;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.my.assembly.MainActivity;
import com.my.assembly.bridge.BridgeHandler;
import com.my.assembly.bridge.CallBackFunction;
import com.qq.wx.voice.recognizer.VoiceRecognizer;
import com.qq.wx.voice.recognizer.VoiceRecognizerListener;
import com.qq.wx.voice.recognizer.VoiceRecognizerResult;
import com.qq.wx.voice.recognizer.VoiceRecordState;
import com.qq.wx.voice.synthesizer.SpeechSynthesizer;

/**
 * <pre>
 *     author : wxq
 *     time   : 2019/01/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class WeChatVoiceHandler implements BridgeHandler, VoiceRecognizerListener {

    private Activity mContext;

    public WeChatVoiceHandler(Activity context) {
        this.mContext = context;
//        VoiceRecognizer.shareInstance().setOpenLogCat(true);
        // 获取中间结果
        SpeechSynthesizer.shareInstance().init(mContext, MainActivity.WX_APPID);
        VoiceRecognizer.shareInstance().setOpenLogCat(true);
        VoiceRecognizer.shareInstance().setSaveVoice(true);
        VoiceRecognizer.shareInstance().setSaveSilk(true);
        // 设置非连续识别
//        VoiceRecognizer.shareInstance().setContReco(true);
//        VoiceRecognizer.shareInstance().setGetPackageMode(true);
        // 需要标点
//        VoiceRecognizer.shareInstance().setResultType(0x01);
        // 设置超时时间(5000毫秒=5秒)
//        VoiceRecognizer.shareInstance().setTimeout(5000);
        VoiceRecognizer.shareInstance().setSilentTime(1000);
        VoiceRecognizer.shareInstance().setListener(this);
        //685f23b7f4c1aaa8b3d46e133bc89c20
        if (VoiceRecognizer.shareInstance().init(mContext, MainActivity.WX_APPID) < 0) {
            //初始化失败
            Toast.makeText(context, "语音初始化失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void handler(String data, CallBackFunction function) {
        Log.i("wxq", "wechatvoice->" + data);
        JsonObject jsonObject = (JsonObject) new JsonParser().parse(data);
        String type = jsonObject.get("type").getAsString();
        if ("start".equals(type)) {
            //开始识别
            int ret = VoiceRecognizer.shareInstance().start();
            Log.i("wxq", "ret->" + ret);
            if (ret < 0) {
                Toast.makeText(mContext, "启动语音识别失败", Toast.LENGTH_SHORT).show();
            }
        } else {
            //结束识别
            VoiceRecognizer.shareInstance().stop();
        }
    }

    @Override
    public void onGetVoiceRecordState(VoiceRecordState voiceRecordState) {
        Log.i("wxq", voiceRecordState.name());
    }

    @Override
    public void onGetResult(VoiceRecognizerResult result) {
        Log.i("wxq", result.text);
//        String res = "";
//        if (result != null && result.words != null) {
//            int wordSize = result.words.size();
//            StringBuilder results = new StringBuilder();
//            for (int i = 0; i < wordSize; ++i) {
//                VoiceRecognizerResult.Word word = (VoiceRecognizerResult.Word) result.words.get(i);
//                if (word != null && word.text != null) {
//                    results.append("\r\n");
//                    results.append(word.text.replace(" ", ""));
//                }
//            }
//            results.append("\r\n");
//            res = results.toString();
//            Log.i("wxq", "res->" + res);
//        }
    }

    @Override
    public void onVolumeChanged(int i) {
        Log.i("wxq", "volume->" + i);
    }

    @Override
    public void onGetError(int i) {
        Log.i("wxq", "error->" + i);
    }

    @Override
    public void onGetVoicePackage(byte[] bytes, String s) {

    }
}
